import router from './router'
import ElEmentUi from 'element-ui'
import { getToken } from './utils/js-storage'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

router.beforeEach((to, from, next) => {
  Nprogress.start()
  // to 进入哪里  from 来自哪里  next 跳转方法
  // next() // 必须使用next() 放行路由
  // 从cookie 获取 token
  const token = getToken()
  if (token) { // 有token
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else { // 无token
    if (to.path === '/login') {
      next()
    } else {
      ElEmentUi.Message({ type: 'error', message: '未登录！！！', duration: 2000 })
      // 跳转到登录
      next({ path: '/login', query: { redirect: to.path } })
    }
  }
})

router.afterEach((to, from) => {
  Nprogress.done()
})
